package cc.aiee.demo.springboot.controller.wxpay;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cc.aiee.core0.bean.J;
import cc.aiee.demo.springboot.basic.constant.ContentType;
import cc.aiee.demo.springboot.controller.wxpay.bean.WxPayRefundQuery;
import cc.aiee.demo.springboot.controller.wxpay.bean.WxPayUnifiedQuery;
import cc.aiee.wxpay.api.WxPayApi;
import cc.aiee.wxpay.core.enums.WxPaySignType;
import cc.aiee.wxpay.core.enums.WxPayTradeType;
import cc.aiee.wxpay.core.pojo.dto.WxPayRefundDTO;
import cc.aiee.wxpay.core.pojo.dto.WxPayUnifiedorderDTO;
import cc.aiee.wxpay.core.pojo.query.WxPayQuery;
import cc.aiee.wxpay.model.WxPayRefundModel;
import cc.aiee.wxpay.model.WxPayUnifiedorderModel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/wx")
@Tag(name = "微信支付")
@Slf4j
public class WxPayController {

	@PostMapping(value = "/refund", consumes = ContentType.FORM)
	@Operation(summary = "退款")
	public @ResponseBody J<WxPayQuery, WxPayRefundDTO> refund(@Valid() WxPayRefundQuery query) {
		WxPayApi api = new WxPayApi();
		WxPayRefundModel model = query.model(new WxPayRefundModel());

		log.info("微信支付退款:请求实体:\n{}", model.toJson(true, false));
		J<WxPayQuery, WxPayRefundDTO> j = api.refund(model);
		return j;
	}
	@PostMapping(value = "/notify", consumes = {ContentType.FORM})
	@Operation(summary = "退款")
	public @ResponseBody J<WxPayQuery, WxPayRefundDTO> refund() {
		return null;
	}

	@PostMapping(value = "/unifiedorder", consumes = ContentType.FORM)
	@Operation(summary = "退款")
	public @ResponseBody J<WxPayQuery, WxPayUnifiedorderDTO> unifiedorder(@Valid() WxPayUnifiedQuery query) {
		WxPayApi api = new WxPayApi();
		WxPayUnifiedorderModel m = query.model(new WxPayUnifiedorderModel());
		m.setBody("力量威共享充电宝");
		m.setSpbill_create_ip("172.18.70.129");
		m.setTrade_type(WxPayTradeType.JSAPI.name());
		m.setSign_type(WxPaySignType.MD5.getType());

		J<WxPayQuery, WxPayUnifiedorderDTO>  j = api.unifiedorder(m);
		return j;
	}
}
